#include "filesystem.h"
#include "ui_filesystem.h"
#include"tcpclient.h"
#include<QInputDialog>
#include<QMessageBox>
#include<QDebug>
#include<QInputDialog>
#include<QFileDialog>
#include<QFileInfo>
#include<QTimer>
#include"tipwidget.h"
#include"messageboxwidget.h"
FileSystem::FileSystem(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FileSystem)
{
    ui->setupUi(this);

    m_fileListWidget=new QListWidget;
    QString styleSheet = R"(
        QListWidget {
            background-color:rgba(243, 253, 255,220);
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 5px;
        }
    )";
    m_fileListWidget->setStyleSheet(styleSheet);
    m_fileListWidget->setIconSize(QSize(35, 35));

    QHBoxLayout *mainLayout=new QHBoxLayout;
    mainLayout->addWidget(m_fileListWidget);
    setLayout(mainLayout);

    connect(m_fileListWidget,&QListWidget::doubleClicked,this,&FileSystem::entryDir); //双击进入文件夹
}

FileSystem::~FileSystem()
{
    delete ui;
}


//新建文件夹
void FileSystem::createDir()
{
    QString newDir=QInputDialog::getText(this,"新建文件夹","新文件夹名：");
    QString createDirName=newDir.trimmed();
    if(!createDirName.isEmpty()){
        if(createDirName.size()>31){
            TipWidget::getInstance()->showTip("文件夹名不能超过31个字符！", TipWidget::Warning, 1000, this);
        }else{
            TcpClient::getInstance().createDir(createDirName);
        }
    }else{
        TipWidget::getInstance()->showTip("文件夹名不能为空！", TipWidget::Warning, 1000, this);
    }
}


//刷新文件夹
void FileSystem::flushDir()
{
    TcpClient::getInstance().flushDir();

}

//更新文件夹列表
void FileSystem::updateFileList(const QMap<QString,int>&fileList)
{
    //清除List
    m_fileListWidget->clear();
    // 设置单选模式
    m_fileListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    // 清除默认选中状态
    m_fileListWidget->clearSelection();
    QListWidgetItem *pItem = NULL;
    for (QMap<QString, int>::const_iterator it = fileList.constBegin(); it != fileList.constEnd(); ++it) {
        pItem = new QListWidgetItem;
        if(it.value()==0){
            pItem->setIcon(QIcon(QPixmap(":/img/dir.png")));
        }else{
            pItem->setIcon(QIcon(QPixmap(":/img/file.png")));
        }
        pItem->setText(it.key());
        QFont font = pItem->font();
        font.setPointSize(15);
        pItem->setFont(font);
        m_fileListWidget->addItem(pItem);
    }
}


//删除文件、文件夹
void FileSystem::delDir_File()
{
    QListWidgetItem *item = m_fileListWidget->currentItem();
    if(item == NULL)
    {
        TipWidget::getInstance()->showTip("请选中需要删除的文件/文件夹！", TipWidget::Warning, 1000, this);
        return;
    }
    QString name = item->text(); // 获取文件/文件夹名
    TcpClient::getInstance().delDir_File(name);
}

//重命名文件、文件夹
void FileSystem::renameDir_File()
{
    QListWidgetItem *item = m_fileListWidget->currentItem();
    if(item == NULL)
    {
        TipWidget::getInstance()->showTip("请选中需要重命名的文件/文件夹", TipWidget::Warning, 1000, this);
        return ;
    }
    QString oldName = item->text();                 // 获取文件/文件夹名
    QString newName=QInputDialog::getText(this,"重命名","请输入新的文件名：");
    newName=newName.trimmed();
    if(!newName.isEmpty()){
        TcpClient::getInstance().renameDir_File(oldName,newName);
    }else{
        TipWidget::getInstance()->showTip("文件名不能为空", TipWidget::Warning, 1000, this);
    }
}

//进入文件夹
void FileSystem::entryDir(const QModelIndex &index)
{
    QString dirName = index.data().toString();
    TcpClient::getInstance().entryDir(dirName);
}

//返回上一级
void FileSystem::backDir()
{
    TcpClient::getInstance().backDir();
}

//上传文件
void FileSystem::uploadFile()
{
    QString uploadFilePath=QFileDialog::getOpenFileName(this,"选择文件");
    if(uploadFilePath.isEmpty())
    {
        TipWidget::getInstance()->showTip("请选择需要上传的文件！", TipWidget::Warning, 1000, this);
        return ;
    }
    TcpClient::getInstance().uploadFile(uploadFilePath);
}


//下载文件
void FileSystem::downLoadFile()
{
    QListWidgetItem *item = m_fileListWidget->currentItem();
    if(item == NULL)
    {
        TipWidget::getInstance()->showTip("请选择需要下载的文件！", TipWidget::Warning, 1000, this);
        return;
    }
    QString name = item->text();
    // 获取保存的位置
    QString saveFilePath = QFileDialog::getSaveFileName(this,"另存为",name);
    if(saveFilePath.isEmpty())
    {
        TipWidget::getInstance()->showTip("请指定下载文件的位置！", TipWidget::Warning, 1000, this);
        return ;
    }
    TcpClient::getInstance().downLoadFile(name,saveFilePath);
}

//移动文件
void FileSystem::moveFile()
{
    QListWidgetItem *item = m_fileListWidget->currentItem();
    if(item == NULL)
    {
        TipWidget::getInstance()->showTip("请选择需要移动的文件！", TipWidget::Warning, 1000, this);
        return;
    }
    QString name = item->text();
    QString moveSourceFilePath=TcpClient::getInstance().curDirPath();
    //源文件路径和文件名
    m_moveSourceFileName=name;
    m_moveSourceFilePath=moveSourceFilePath;
    emit setStatus(true);
    TipWidget::getInstance()->showTip("请跳转到需要移动到的目录，\n然后选择目标目录", TipWidget::Info, 2000, this);
}


//选择移动文件的目标路径
void FileSystem::selectTargetPath()
{
    //目标文件路径
    QString targetPath=TcpClient::getInstance().curDirPath();
    MessageBoxWidget *widget=new MessageBoxWidget;
    widget->setMessageBox(QString("您确认将 %1 的 %2 文件  移动到 %3 目录下吗？")
                              .arg(m_moveSourceFilePath).arg(m_moveSourceFileName).arg(targetPath),
                          MessageBoxWidget::Info
                          ,this);
    connect(widget,&MessageBoxWidget::confirmSignal,this,[=](){
        TcpClient::getInstance().moveFile(m_moveSourceFilePath,m_moveSourceFileName,targetPath);
        m_moveSourceFilePath.clear();
        m_moveSourceFileName.clear();
        emit setStatus(false);
    });
    connect(widget,&MessageBoxWidget::cancelSignal,this,[=](){
        m_moveSourceFilePath.clear();
        m_moveSourceFileName.clear();
        emit setStatus(false);
    });
    widget->show();
}

void FileSystem::shareFile()
{
    QListWidgetItem *item = m_fileListWidget->currentItem();
    if(item == NULL)
    {
        TipWidget::getInstance()->showTip("请选择需要分享的文件！", TipWidget::Warning, 1000, this);
        return;
    }
    QString name = item->text();
    if(!m_shareDialog){
        m_shareDialog=new ShareFileDialog;
        connect(m_shareDialog, &QObject::destroyed, this, [=]() {
            m_shareDialog = nullptr;
        });
        m_shareDialog->show();

        connect(this,&FileSystem::sendFriendList,m_shareDialog,&ShareFileDialog::getFriendList);
        connect(m_shareDialog,&ShareFileDialog::sendShareList,[=](const QList<QString>&shareList){
            //分享文件操作
            TcpClient::getInstance().shareFile(shareList,name);
        });
        TcpClient::getInstance().getOnlineFriend();
    }
}

void FileSystem::onlineFriendList(const QList<QString> &usrList)
{
    emit sendFriendList(usrList);
}

